#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui>
#include <QTcpSocket>
#include <QTcpServer>
#include <QtEndian>
#include "oscartypes.h"

namespace Ui {
class MainWindow;
}

using namespace OSCAR;

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void parseFLAP(QByteArray &ba, bool outcoming);
    u08 readU08(QByteArray &ba);
    u16 readU16(QByteArray &ba);
    u32 readU32(QByteArray &ba);
    QString readString(QByteArray &ba, int length);
    QByteArray readBytes(QByteArray &ba, int length);
    TLV__Header readTLVHeader(QByteArray &ba);
    bool readTLV(QByteArray &ba);
    SNAC__Header readSnacHeader(QByteArray &ba);
    void readSnac(QByteArray &ba);
    bool readSsiItem(QByteArray &ba);
    
private:
    Ui::MainWindow *ui;
    QPlainTextEdit *logEdit;

    QTcpSocket *socket;
    QTcpServer *server;
    QTcpSocket *servSocket;

    QByteArray inBuffer;
    QByteArray outBuffer;

    QString curColor;
    QString BOS;

    enum {ctLogin, ctBOS} connectionType;

public slots:
//    void connectToServer(const QString &Server, quint16 port);
    void connectToServer(QString host, quint16 port);
    void disconnectFromServer();

    void logSvcMessage(QString msg);
    void logMessage(QString msg, QString color = "");
    void logOutBytes(QByteArray &ba);
    void logInBytes(QByteArray &ba);
    void logError(QString text);
    QString ba2str(QByteArray &ba);

private slots:
    //void onTimer();
    void onSocketConnected();
    void onSocketDisconnected();
    void onSocketReadyRead();
    void onSocketError(QAbstractSocket::SocketError socketError);

    void onServerConnected();
    void onServerDisconnected();
    void onServerRead();
};

#endif // MAINWINDOW_H
